package com.yunbiao.ybbusstopplate.refactoring.ui.dalian;

import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.example.library_hy_advert.system.Cache;
import com.yunbiao.ybbusstopplate.R;
import com.yunbiao.ybbusstopplate.base.BaseFragment;
import com.yunbiao.ybbusstopplate.base.BaseSettingFragment;
import com.yunbiao.ybbusstopplate.databinding.FragmentDalianHeadInfoBinding;
import com.yunbiao.ybbusstopplate.refactoring.serial.SerialPortServices;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DalianHeadInfoFragment extends BaseFragment {
    private FragmentDalianHeadInfoBinding binding;

    private static final String TAG = "HeadInfoFragment";

    @Override
    public int getViewId() {
        return R.layout.fragment_dalian_head_info;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.bind(requireView());

        binding.vSetting.setOnClickListener(v -> {
            FragmentUtils.add(getChildFragmentManager(),new DalianSettingFragment(),binding.flSetting.getId());
        });

        String string = Cache.getString(ZHIDAParser.KEY_FIXED_PROMOTION_SLOGAN);
        if(TextUtils.isEmpty(string)){
            UiMessageUtils.getInstance().addListener(ZHIDAParser.Event.HEAD_UPDATE_PROMOTION_SLOGAN.getType(),uiMessageCallback);
            UiMessageUtils.getInstance().addListener(ZHIDAParser.Event.HEAD_BROAD_SITE.getType(),uiMessageCallback);
        } else {
            binding.tvFixedPromotionSlogan.post(() -> {
                TextPaint paint = binding.tvFixedPromotionSlogan.getPaint();
                float v = paint.measureText(string, 0, string.length());
                if(v > ScreenUtils.getAppScreenWidth()){
                    binding.stvText.setText(string);
                    binding.stvText.setVisibility(View.VISIBLE);
                    binding.tvFixedPromotionSlogan.setText("");
                    binding.tvFixedPromotionSlogan.setVisibility(View.GONE);
                } else {
                    binding.stvText.setVisibility(View.GONE);
                    binding.tvFixedPromotionSlogan.setVisibility(View.VISIBLE);
                    binding.tvFixedPromotionSlogan.setText(string);
                }
            });
        }
    }

    @Override
    protected void initData() {
        requireView().postDelayed(SerialPortServices::start,1000);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(disposable != null && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        UiMessageUtils.getInstance().removeListener(ZHIDAParser.Event.HEAD_UPDATE_PROMOTION_SLOGAN.getType(),uiMessageCallback);
        UiMessageUtils.getInstance().removeListener(ZHIDAParser.Event.HEAD_BROAD_SITE.getType(),uiMessageCallback);
    }

    private String mTempPromotionSlogan;

    private final UiMessageUtils.UiMessageCallback uiMessageCallback = new UiMessageUtils.UiMessageCallback() {
        @Override
        public void handleMessage(@NonNull UiMessageUtils.UiMessage localMessage) {
            if(localMessage.getId() == ZHIDAParser.Event.HEAD_UPDATE_PROMOTION_SLOGAN.getType()){
                mTempPromotionSlogan = (String) localMessage.getObject();
                binding.stvText.setText(mTempPromotionSlogan);
                binding.stvText.setVisibility(View.VISIBLE);
                binding.tvFixedPromotionSlogan.setVisibility(View.GONE);
                binding.tvFixedPromotionSlogan.setText("");
            } else if(localMessage.getId() == ZHIDAParser.Event.HEAD_BROAD_SITE.getType()){
                Object[] objects = (Object[]) localMessage.getObject();
                String message = objects[0].toString();
                boolean isIn = (boolean) objects[1];
                playTemp(message,isIn ? 2 : 1);
            }
        }
    };

    private Disposable disposable;
    private void playTemp(String text,int minutes){
        if(disposable != null && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        disposable = Observable.timer(minutes * 30L, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> binding.stvText.setText(text))
                .doOnTerminate(() -> binding.stvText.setText(mTempPromotionSlogan))
                .subscribe();
    }

    private long mExitTime = 0;
    @Override
    public boolean onBackClick() {
        List<Fragment> fragments = FragmentUtils.getFragments(getChildFragmentManager());
        for (int i = fragments.size() - 1; i >= 0; i--) {
            Fragment fragment = fragments.get(i);
            if(fragment instanceof BaseSettingFragment){
                FragmentUtils.remove(fragment);
                return true;
            }
        }
        return false;
    }
}
